<template>
  <div id="App">

    <div style="display: flex;justify-content: center;align-items: center;">
      <div class="drop">
        <img style="width: 70px;height: 70px;left: 16px;top: 16px" :src="require('@/static/2校徽.png')"></img>
      </div>
<!--      <script src="https://kit.fontawesome.com/f23451c27c.js" crossorigin="anonymous"></script>-->
    </div>

    <div class="c1">
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="c2">
        <!--用户名-->
        <h></h>
        <el-form-item label="" prop="username">
          <el-input v-model="loginForm.username" placeholder="学号" prefix-icon="" onfocus="this.placeholder=''"
                    onblur="this.placeholder='学号'" maxlength="9" clearable></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item label="" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="登录密码" prefix-icon=""
                    onfocus="this.placeholder=''" onblur="this.placeholder='密码'" maxlength="18" show-password>
          </el-input>
        </el-form-item>
        <el-form-item label="" prop="verificationCode">
          <el-input v-model="loginForm.verificationCode" placeholder="请输入验证码" clearable
                    onfocus="this.placeholder=''" onblur="this.placeholder='请输入验证码'"
                    onkeyup="value=value.replace(/[^\w\.\/]/ig,'')" maxlength="4">
          </el-input>
          <div @click="refreshCode" style="top: 20px;left: 20px">
            <!--验证码组件-->
            <SIdentify :identifyCode="identifyCode"></SIdentify>
          </div>
        </el-form-item>
        <el-form-item>
        <el-button style="right: 40px" type="text" @click="findPw">忘记密码?</el-button>
          <el-button style="left: 20px" type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>


<script>

import SIdentify from '../components/yzcode.vue'
export default {
  name: 'Login',
  components: {
    SIdentify
  },
  data() {
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        username: '',
        password: '',
        verificationCode: ''
      },
      // 登录表单的验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入学号', trigger: 'blur' },
          // { min: 5, max: 9, message: '长度非法！', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        verificationCode: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      },
      identifyCode: '',
      identifyCodes: '1234567890abcdefjhijklinopqrsduvwxyz',
    }
  },
  methods: {
    // 刷新验证码
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length)]
      }
    },
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    },
    // 登录
    login() {

      // 简单的学号密码验证
      //判断学号或密码是否为空
      if (this.loginForm.username === '' || this.loginForm.password === '') {
        this.$msgbox.alert('登录信息不能为空！')
        // this.$refs.loginFormRef.resetFields()
      }
      else {
        //判断学号长度是否符合
        if (this.loginForm.username.length !== 9) {
          // this.$refs.loginFormRef.resetFields()
          this.$msgbox.alert('学号格式错误！')
        } else {
          //判断验证码是否为空
          if (this.loginForm.verificationCode === '') {
            this.$msgbox.alert('验证码不能为空')
          } else {
            //判断验证码是否输入正确
            if (this.loginForm.verificationCode.toLowerCase() !== this.identifyCode.toLowerCase()) {
              this.$msgbox.alert('验证码错误')
              this.loginForm.verificationCode = ''
              this.refreshCode()

            } else {
              this.$axios.post(`http://localhost:/login/findStatus/${this.loginForm.username}&${this.loginForm.password}`).then(res => {
                console.log(res.data)
                window.sessionStorage.setItem("stuNum",res.data.username)
                window.sessionStorage.setItem("status",res.data.status)
                window.sessionStorage.setItem("address",res.data.address)
                window.sessionStorage.setItem("phone",res.data.phone)
                window.sessionStorage.setItem("classNum",res.data.classNum)
                window.sessionStorage.setItem("college",res.data.college)
                window.sessionStorage.setItem("major",res.data.major)
                window.sessionStorage.setItem("name",res.data.name)
              }),
              this.$axios.post(`http://localhost:/login/findByName/${this.loginForm.username}&${this.loginForm.password}`, {username:this.loginForm.username,password:this.loginForm.password}).then(res => {
                // console.log(res)
                if (res.status !== 200) return console.log("连接失败");
                if (res.data === true) {
                  this.$message.success('登录成功')

                  // this.$refs.loginFormRef.resetFields()
                  this.$router.push('/Home')
                }
                if(res.data === false){
                  this.$message.error("登录失败！")
                }
              })

            }
          }

        }
      }


    },
    findPw() {
      this.$refs.loginFormRef.resetFields()
      this.$router.push('/ForgetPw')
    }
  },
  mounted() {
    // 初始化验证码
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)
  },
}
</script>

<style scoped>

.drop {
  top:40px;
  position: relative;
  width: 100px;
  height: 100px;
  box-shadow: inset 10px 10px 10px rgba(0, 0, 0, 0.05), 15px 25px 10px rgba(0, 0, 0, 0.05), 15px 20px 20px rgba(0, 0, 0, 0.05);
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.drop::before{
  content: '';
  position: absolute;
  top: 25px;
  left: 15px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #fff;
}
.drop::after{
  content: "";
  position: absolute;
  top: 20px;
  left: 30px;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #fff;
}





/*总观*/
#App{
  margin: 0;
  font-family: "PingFang SC","Microsoft Yahei",sans-serif;
  /*背景*/
  background: url(/src/assets/gduf.jpg) fixed no-repeat;
  background-size: cover;
}



/*div样式*/
.c1{
  /*视窗大小*/
  /* width: 100vw; */
  height: 85vh;
  /*居中*/
  display: flex;
  align-items: center;
  justify-content: center;

}
/*表格样式*/
.c2{
  /*表格大小*/
  width: 240px;
  height: 280px;
  /*排版*/
  display: flex;
  flex-direction: column;
  /*样式*/
  padding: 40px;
  text-align: center;
  position: relative;
  z-index: 100;
  background: inherit;
  border-radius: 18px;
  overflow: hidden;
}
/*h2*/
.c2 h2{
  font-size: 18px;
  font-weight: 400;
  color: #3d5245;
}
/*placeholder*/
.c2 input::placeholder{
  color: #3d5245;
}
/*基本样式*/
.c2 input,
.c2 button{
  margin: 6px 0;
  height: 36px;
  border: none;
  background-color: rgba(255, 255, 255, 0.3);
  border-radius: 4px;
  padding:0 14px;
  color: #3d5245;
}
/*清除点击框后的边框 美观*/
.c2 button:focus,
.c2 input:focus{
  outline: 0;
}
/*button样式*/
.c2 button{
  margin-top: 24px;
  background-color: rgba(160, 100, 90, 0.7);
  color: white;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  transition: 0.4s;
}
/*button鼠标悬浮高亮*/
.c2 button:hover{
  background-color: rgba(115, 46, 34, 0.7);
}

/*毛玻璃背景*/
.c2::before{
  content: "";
  width: calc(100% + 20px);
  height: calc(100% + 20px);
  position: absolute;
  top: -10px;
  left: -10px;
  background: inherit;
  box-shadow: inset 0 0 0 200px rgba(255, 255, 255, 0.25);
  filter: blur(5px);

  overflow: hidden;
  z-index: -1;
}

/*滑动光片*/
.c2 button::before,
.c2 button::after{
  content: "";
  display: block;
  width: 80px;
  height: 100%;
  background: rgba(179, 255, 210, 0.5);
  opacity: 0.5;
  position: absolute;
  top: 0;
  left: 0;
  transform: skewX(-15deg);
  filter: blur(10px);
  overflow: hidden;
  transform: translateX(-100px);
}
/*双重划片增加视觉 把后面的提前*/
.c2 button::after{
  width: 40px;
  background: rgba(179, 255, 210, 0.3);
  left: 60px;
  opacity: 0;
  filter: blur(5px);
}
/*滑片动画*/
.c2 button:hover::before{
  transition: 1s;
  transform: translateX(320px);
  opacity: 0.7s;
}
.c2 button:hover::after{
  transition: 1s;
  transform: translateX(320px);
  opacity: 1;
}


</style>
